package com.ncs.controller.manager_controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ncs.entity.PageResult;
import com.ncs.entity.QueryPageBean;
import com.ncs.entity.Result;
import com.ncs.config.MyWebMvcConfigurer;

import com.ncs.mapper.GalleryMapper;
import com.ncs.pojo.Gallery;
import com.ncs.pojo.PhoneManager;
import com.ncs.pojo.Table;
import com.ncs.service.GalleryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

@CrossOrigin
@RestController
@RequestMapping("/manager/gallery")
public class ManageGalleryController {
    @Autowired
    GalleryService galleryService;

    @Value("${picture.upload.location}")
    private String pictuerUploadPath;

    @Resource
    private GalleryMapper galleryMapper;

    @PostMapping("/getpage")
    /**分页查询**/
    Result getPage(@RequestBody QueryPageBean queryPageBean){
        Result result = new Result();
        try {
//            当前页
            Integer currentPage = queryPageBean.getCurrentPage();
//            页面大小
            Integer pageSize = queryPageBean.getPageSize();
//            分页查询
            Page<Gallery> adminPage = new Page<Gallery>(currentPage, pageSize);
            QueryWrapper<Gallery> adminQueryWrapper = new QueryWrapper<>();
            if (queryPageBean.getQueryString() != null) {
                adminQueryWrapper.like("phone_number", queryPageBean.getQueryString());
            }
            Page<Gallery> page = galleryService.page(adminPage, adminQueryWrapper);
//            设置分页查询返回值
            PageResult pageResult = new PageResult();
            pageResult.setRows(page.getRecords());
            pageResult.setTotal(page.getTotal());
            result.setData(pageResult);
            result.setFlag(true);
            result.setMessage("查询成功");

        }catch (Exception e){
//            异常处理
            result.setFlag(false);
            result.setMessage(e.getMessage());
        }
        return  result;

    }
    @PostMapping("/upload")
    /**分页查询**/
    public String toUpdate(@RequestBody MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        String type = FileUtil.extName(originalFilename);

        String uuid = IdUtil.fastSimpleUUID();
        String fileUUid = uuid + StrUtil.DOT + type;

        //储存于磁盘上的地址
        File uploadPicture = new File(pictuerUploadPath + fileUUid);
        File parentFile = uploadPicture.getParentFile();
        if (!parentFile.exists()){
            parentFile.mkdirs();
        }
        file.transferTo(uploadPicture);
        String url = "http://localhost:8081/manager/gallery/" + fileUUid;


        Gallery saveGallery = new Gallery();
//        saveGallery.setPicture(url);
//        galleryMapper.insert(saveGallery);
        return url;
    }

    @PostMapping("/toUpdate")
    /**分页查询**/
    Result toUpdate(@RequestBody Gallery gallery){
        Result result = new Result();
        try {
            UpdateWrapper<Gallery> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("id",gallery.getId());
            galleryService.update(gallery,updateWrapper);
            result.setFlag(true);
            result.setMessage("更新成功");

        }catch (Exception e){
//            异常处理
            result.setFlag(false);
            result.setMessage("更新失败");
        }
        return  result;

    }



}
